1. Import the numpy package under the name np
In [2]:
import numpy as np
In [3]:
try:
np
except NameError:
print('Numpy not correctly imported')
2. Create a null vector named Z
of size 10. Don't use [0, 0, ...] notation.
In [10]:
Z = np.zeros(10)
In [6]:
assert type(Z).__module__ == np.__name__
assert len(Z) == 10
assert sum(Z) == 0
3. Create a null vector Z
of size 10 but the fifth value which is 1
In [14]:
Z[4] = 1
Z
Out[14]:
In [13]:
assert type(Z).__module__ == np.__name__
assert len(Z) == 10
assert sum(Z) == 1
4. Create a Numpy vector Z
with values ranging from 10 to 49
In [15]:
Z = np.arange(10, 50)
In [16]:
assert type(Z).__module__ == np.__name__
assert len(Z) == 40
assert sum(Z) == 1180
5. Reverse the vector from the previous task (first element becomes last)
In [17]:
Z
Out[17]:
In [21]:
Z = Z[::-1]
In [22]:
assert type(Z).__module__ == np.__name__
assert len(Z) == 40
assert sum(Z) == 1180
assert Z[0] == 49
assert Z[-1] == 10
6. Create a 3x3 matrix Z
with values ranging from 0 to 8
In [23]:
Z = np.arange(9).reshape((3, 3))
In [24]:
assert Z.shape == (3, 3)
assert np.all(sum(Z) == np.array([9, 12, 15]))
7. Find the non-zero elements from [1,2,0,0,4,0] and store the result in nz
In [50]:
Z = np.array([1,2,0,0,4,0])
Z != 0
Out[50]:
In [56]:
Z[np.array([ False, False, False, False, True, False], dtype=bool)]
Out[56]:
In [48]:
assert np.all(nz == np.array([1, 2, 4]))
8. Create a 3x3x3 (i.e. three dimensions with three values each) array Z
with random values.`
In [58]:
assert Z.shape == (3, 3, 3)
13. Create a 10x10 array Z
with random values and find the minimum and maximum values and store them in Zmin
and Zmax
.
In [70]:
Z = np.random.random((10, 10))
Zmax, Zmin = Z.max(), Z.min()
In [71]:
z = Z.ravel()
idx = z.argsort()
assert z[idx[0]] == Zmin
assert z[idx[-1]] == Zmax
14. Create a random vector Z
of size 30 and find the mean value using Numpy. Store the result into mean
In [ ]:
In [ ]:
accumulative = 0
for z in Z:
accumulative += z
assert mean - (accumulative/len(Z)) < 0.0001
15. Create a 8x8 matrix Z
and fill it with a chessboard pattern (say, 1 == 'black' and 0 == 'white'). Use fancy indexing.
In [ ]:
16. Multiply a 5x3 matrix by a 3x2 matrix (content is insignificant).
In [ ]:
17. Given a array np.arange(11)
, negate all elements which are between 3 and 8, in place.
In [ ]: